const mongoose = require('mongoose');
const Cart = require('../models/ShoppingCart.model');

//新增
exports.create=function(req,res,next){
    // if(req.body.parentId){
    //     this.
    // }
    const cart = new Cart(req.body);
    cart.save().then((data)=>{
        res.json(data);
    })
}

exports.update=function(req,res,next){
    const id = req.params.id;
    Cart.findByIdAndUpdate(id,{$set:req.body},{new:false}).then(data=>{
        res.json(data);
    })
}


exports.remove=function(req,res,next){
    const id = req.params.id;
    Cart.findByIdAndDelete(id,(err,data)=>{
        res.json(data);
    })
}

exports.list=function(req,res,next){
    var page = req.body.page?req.body.page:1;
    var rows = req.body.rows?req.body.rows:1;

    var queryCondition = {};  //查询条件
    if(req.body.userId&&req.body.userId.trim().length>0){
        userId         = req.body.userId;
        queryCondition = {
            "userId": new RegExp(userId,'i')
        }
    }
    Cart.paginate(queryCondition,{page: + page,limit: + rows},function(err,result){
        result.rows = result.docs;
        delete result.docs;
        res.json(result);
    })
}

exports.getdata=function(req,res,next){
    var id = req.params.id;
    Cart.findById(id).then(data=>{
        res.json(data);
    })
}
exports.removes=function(req,res,next){
    var ids = req.body.ids.split(',');
    if(ids.length>0){
        Cart.remove({_id:{$in:ids}}).then(data=>{
            res.json({'message':'delete ok',status:200})
        })
    }

}